package com.example.demo.lambda;

import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.function.Function;
import java.util.function.IntBinaryOperator;
import java.util.function.IntConsumer;
import java.util.function.IntPredicate;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 马拉圈
 * Date: 2023-11-20
 * Time: 20:47
 */
public class LambdaDemo {

    public static void main1(String[] args) {
        new Thread(() -> {
            System.out.println("666");
        }).start();
        System.out.println("777");
    }

    public static void main2(String[] args) {
        int a = 111;
        int b = 222;
        new Thread(() -> {
            System.out.println(calculateNum(a, b, (left, right) -> left + right));
        }).start();
        System.out.println(calculateNum(a, b, (left, right) -> Math.abs(left - right)));
    }

    public static int calculateNum(int a, int b, IntBinaryOperator operator) {
        return operator.applyAsInt(a, b);
    }

    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        printNumAnd(arr, x -> x % 2 == 0);
        System.out.println("------------------------------------------");
        printNumAnd(arr, x -> x % 2 == 0, x -> x > 5);
        System.out.println("------------------------------------------");
        printNumAnd(arr, x -> x % 2 == 0, x -> x > 5, x -> x < 9);
        System.out.println("------------------------------------------");
        printNumOr(arr, x -> x % 2 == 0);
        System.out.println("------------------------------------------");
        printNumOr(arr, x -> x % 2 == 0, x -> x > 5);
        System.out.println("------------------------------------------");
        printNumOr(arr, x -> x % 2 == 0, x -> x > 5, x -> x < 9);
        System.out.println("------------------------------------------");
        printNumNegate(arr, x -> x % 2 == 0);
        System.out.println("------------------------------------------");
        printNumNegate(arr, x -> x % 2 == 0, x -> x > 5);
        System.out.println("------------------------------------------");
        printNumNegate(arr, x -> x % 2 == 0, x -> x > 5, x -> x < 9);
        System.out.println("------------------------------------------");
    }

    public static void printNumAnd(int[] arr, IntPredicate... predicates) {
        IntPredicate predicate = Arrays.stream(predicates)
                .reduce((x1, x2) -> x1.and(x2))
                .orElse(null);
        Arrays.stream(arr)
                .filter(predicate)
                .forEach(x -> System.out.println(x));
        ;
    }

    public static void printNumOr(int[] arr, IntPredicate... predicates) {
        IntPredicate predicate = Arrays.stream(predicates)
                .reduce((x1, x2) -> x1.or(x2))
                .orElse(null);
        Arrays.stream(arr)
                .filter(predicate)
                .forEach(x -> System.out.println(x));
        ;
    }

    public static void printNumNegate(int[] arr, IntPredicate... predicates) {
        IntPredicate predicate = Arrays.stream(predicates)
                .reduce((x1, x2) -> x1.or(x2))
                .orElse(null);
        Arrays.stream(arr)
                .filter(predicate.negate())
                .forEach(x -> System.out.println(x));
        ;
    }

    public static void main4(String[] args) {
        int ret = typeConver("123456", str -> Integer.parseInt(str));
        System.out.println(ret * 10);
        String result = typeConver("123456", str -> str + str);
        System.out.println(result);
    }

    public static <R> R typeConver(String str, Function<String, R> function) {
        return function.apply(str);
    }

    public static void main5(String[] args) {
        int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        foreachArr(arr, x -> System.out.println(x % 2 == 0 ? (x + 2) / 2 : (x + 3) / 2));

    }


    public static void foreachArr(int[] arr, IntConsumer consumer) {
        for(int x : arr) {
            consumer.accept(x);
        }
    }

}
